<template>

  <el-form :inline="true" :model="formData" class="demo-form-inline">
        <el-form-item >
            <CategoryTree v-model="formData.categoryId" title="分类"></CategoryTree>
        </el-form-item>
        <enum  v-model="formData.status" enumName="ProductStatus" :isAll0="true"></enum>
        <el-form-item>
            <el-input v-model="formData.name" placeholder="请输入关键字" clearable />
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="exportExcel">导出 </el-button>
        </el-form-item>
    </el-form>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column fixed prop="id" label="id" width="150" />
      <el-table-column prop="name" label="Name" width="120" />
      <el-table-column prop="subName" label="subName" width="120" show-overflow-tooltip/>
      <el-table-column prop="img" label="img" width="120">
        <template #default="scope">
          <img :src="scope.row.img[0]" width="50" height="50" />
        </template>
      </el-table-column>
      <el-table-column prop="categoryName" label="categoryId" width="600" />
      <el-table-column prop="statusX" label="Zip" width="120" />
      <el-table-column prop="lastUpdateBy" label="Zip" width="120"  :formatter="formatter"/>
      <el-table-column prop="lastUpdateTime" label="Zip" width="120" :formatter="formatter"/> 
      <el-table-column fixed="right" label="Operations" min-width="120">
        <template #default="scope">
          <el-button  type="primary" size="small" v-if="scope.row.status!=120" @click="edit(scope.row)">编辑</el-button>
          <el-button  type="primary" size="small" @click="handleClick(scope.row, 1)" v-if="scope.row.status==2">上架</el-button>
          <el-button  type="primary" size="small" @click="handleClick(scope.row, 2)" v-if="scope.row.status==1">下架</el-button>
          <el-button  type="primary" size="small" @click="handleClick(scope.row, 120)" v-if="scope.row.status==2">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination background layout="total, prev, pager, next" :total="pageData.total" :page-size="pageData.pageSize"
    @current-change="pageChange">
    </el-pagination>
  </template>
  
  <script lang="ts" setup>
  import { ref ,reactive,onMounted } from 'vue'
  import { productApi } from '@/api'
  import { dayjs , ElMessageBox, ElMessage, rowContextKey} from 'element-plus'
  import Enum from '@/components/Enum.vue';
  import CategoryTree from '@/components/CategoryTree.vue';
  import Edit from './Edit.vue';
  import {useRoute ,useRouter} from 'vue-router'
  const router = useRouter()
  const route = useRoute()
  const tableData = ref([])
  const ProductStatus = ref("ProductStatus")
 
 const edit=(row: any) => {
  router.push({name: 'product.edit',query: { id: row.id }})
  }
  const pageData = reactive({
    total: 0,
    pageSize: 5,
    pageNum: 1

})

  const callProductApi=()=>{
    let params={
      pageNum:pageData.pageNum,
      pageSize:pageData.pageSize,
      name:formData.name==""?undefined:formData.name,
      categoryId:formData.categoryId==0?undefined:formData.categoryId,
      status:formData.status==0?undefined:formData.status
    }

    productApi.select.call(params).then(res=>{
        pageData.total = res.count
        tableData.value=res.items
    })
  }
  const pageChange=(pageNum:number)=>{
    pageData.pageNum=pageNum
    callProductApi()
  }
  const formatter = (row: any, column: any, cellValue: any, index: number) => {

    if (column.property === 'lastUpdateTime') {
    return dayjs(cellValue).format('YY年MM月DD HH:mm')
    } else if (column.property === 'lastUpdateBy') {
    return cellValue + " 同志"
    }
    return cellValue
}

  onMounted(() => {
    callProductApi()
  
    })
 const handleClick = (row: any, newStatus: number) => {
    let title = ''
    switch (newStatus){
    case 1:
      title='确认上架'+row.name+'?'
      break
    case 2:
      title='确认下架'+row.name+'?'
      break
    case 120:
      title='确认删除'+row.name+'?'
      break
    }
    ElMessageBox.confirm(
        title,
        '警告',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    ).then(() => {
      callProductStatusApi(row.id, newStatus)
    }).catch(() => {

    });

   }
  
const callProductStatusApi=(id:number, newStatus: number)=>{
    productApi.update.call({id:id,status:newStatus}).then((res :any)=>{
      ElMessage({
        message: '操作成功',
        type: 'success',
      })
      callProductApi()
    })
}
  const formData = reactive({
    name: '',
    categoryId: 0,
    status:0
  })

  const categoryChange = (value:number) => {
    callProductApi()
  }
  const onSubmit = () => {
    callProductApi()
  }
const exportExcel = () => {
window.open('/api/product/download' , '_blank', "")

}
  </script>